package com.situ.stl.dao;

import com.situ.stl.entity.Reserve;
import java.util.List;

import org.apache.ibatis.annotations.*;

/** 
 * Dao层 - 预约
 */
@Mapper 
public interface ReserveMapper{

    /**
     * Insert
     */
    @Insert( value = "insert into `reserve` ( `course_id`, `member_id`) value ( #{courseId}, #{memberId})")
    @Options( useGeneratedKeys = true, keyColumn = "id", keyProperty = "id") 
    int insert( Reserve reserve) ;

    /** 
     * Delete
     */
    @Delete( value = "delete from `reserve` where `id` = #{id}") 
    int delete( Integer id) ;

    /** 
     * Update
     */
    @Update( value = {
        "<script>",
        "update `reserve`",
        "<set>",
        "<if test='status!=null'> `status` = #{status},</if>",
        "<if test='courseId!=null'> `course_id` = #{courseId},</if>",
        "<if test='memberId!=null'> `member_id` = #{memberId},</if>",
        " `score` = #{score},",
        " `comment` = #{comment},",
        " `comment_time` = #{commentTime},",
        "</set>",
        " where `id` = #{id}",
        "</script>"}) 
    int update( Reserve reserve) ;

    @Update({
            "<script>",
            "update `reserve`",
            "<set>",
            " `status` = #{status},",
            "</set>",
            " where `id` in ",
            "   <foreach collection='reserves' item='reserve' open='(' close=')' separator=','>#{reserve.id}</foreach>",
            "</script>"})
    int batchUpdate(@Param("reserves") List<Reserve> reserves, @Param("status") Integer status);

    /** 
     * Select All
     */
    @Select( value = "select * from `reserve`") 
    @Results( value = {
        @Result( column = "id", property = "id", id = true),
        @Result( column = "status", property = "status"),
        @Result( column = "score", property = "score"),
        @Result( column = "comment", property = "comment"),
        @Result( column = "comment_time", property = "commentTime"),
        @Result( column = "reserve_time", property = "reserveTime"),
        @Result( column = "course_id", property = "courseId"),
        @Result( column = "member_id", property = "memberId"),
        @Result( column = "course_id", property = "course", one = @One( select = "com.situ.stl.dao.CourseMapper.selectByPrimaryKey")),
        @Result( column = "member_id", property = "member", one = @One( select = "com.situ.stl.dao.MemberMapper.selectByPrimaryKey"))})
    List<Reserve> selectAll() ;

    /** 
     * Select by primary key
     */
    @Select( value = "select * from `reserve` where `id` = #{id}") 
    @Results( value = {
        @Result( column = "id", property = "id", id = true),
        @Result( column = "status", property = "status"),
        @Result( column = "score", property = "score"),
        @Result( column = "comment", property = "comment"),
        @Result( column = "comment_time", property = "commentTime"),
        @Result( column = "reserve_time", property = "reserveTime"),
        @Result( column = "course_id", property = "courseId"),
        @Result( column = "member_id", property = "memberId"),
        @Result( column = "course_id", property = "course", one = @One( select = "com.situ.stl.dao.CourseMapper.selectByPrimaryKey")),
        @Result( column = "member_id", property = "member", one = @One( select = "com.situ.stl.dao.MemberMapper.selectByPrimaryKey"))})
    Reserve selectByPrimaryKey( int id) ;

    /** 
     * Select
     */
    @Select( value = {
        "<script>",
        "select * from `reserve`",
        "<where>",
        "<if test='status!=null'> and `status` = #{status}</if>",
        "<if test='score!=null'> and `score` = #{score}</if>",
        "<if test='comment!=null and comment.length>0'> and `comment` like '%${comment}%'</if>",
        "<if test='commentTime!=null'> and `comment_time` = #{commentTime}</if>",
        "<if test='reserveTime!=null'> and `reserve_time` = #{reserveTime}</if>",
        "<if test='courseId!=null'> and `course_id` = #{courseId}</if>",
        "<if test='memberId!=null'> and `member_id` = #{memberId}</if>",
        "</where>",
        "order by reserve_time desc",
        "</script>"}) 
    @Results( value = {
        @Result( column = "id", property = "id", id = true),
        @Result( column = "status", property = "status"),
        @Result( column = "score", property = "score"),
        @Result( column = "comment", property = "comment"),
        @Result( column = "comment_time", property = "commentTime"),
        @Result( column = "reserve_time", property = "reserveTime"),
        @Result( column = "course_id", property = "courseId"),
        @Result( column = "member_id", property = "memberId"),
        @Result( column = "course_id", property = "course", one = @One( select = "com.situ.stl.dao.CourseMapper.selectByPrimaryKey")),
        @Result( column = "member_id", property = "member", one = @One( select = "com.situ.stl.dao.MemberMapper.selectByPrimaryKey"))})
    List<Reserve> select( Reserve reserve) ;

    /**
     * 根据会员和课程查询
     * @param reserve
     * @return
     */
    @Select({
            "select * from reserve",
            "where member_id=#{memberId} and course_id=#{courseId}",
    })
    Reserve selectByMemberAndCourse(Reserve reserve);

    @Select({
            "select * from reserve",
            "where member_id=#{memberId}",
    })
    List<Reserve> selectByMember(int memberId);

    @Select({
            "select * from reserve",
            "where course_id=#{courseId}",
    })
    List<Reserve> selectByCourse(int courseId);

}